package com.xhkj.testpose

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        checkPermission()
        findViewById<TextView>(R.id.clickStart).setOnClickListener {
            startActivity(Intent(this, PostActivity::class.java))
//            CrashReport.testJavaCrash();
        }
        findViewById<TextView>(R.id.clickStartFace).setOnClickListener {
            startActivity(Intent(this, FaceActivity::class.java))
//            CrashReport.testJavaCrash();
        }
    }

    /**
     * 请求权限
     */
    private fun checkPermission() {
        val newList: MutableList<String> = mutableListOf()  //里面装未允许的权限列表

        val permList = arrayOf(
            Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO,
            Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH_CONNECT
        )

        for (perm in permList) {
            //未允许的权限
            if (ActivityCompat.checkSelfPermission(
                    this, perm
                ) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.shouldShowRequestPermissionRationale(this, perm)
            ) {
                newList.add(perm)
            }
        }
        if (newList.size > 0) {
            ActivityCompat.requestPermissions(this, newList.toTypedArray(), 0)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String>, grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
}